package com.txzt.gaea.business.project.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.project.domain.model.ProjectFund;
import com.txzt.gaea.business.project.service.IProjectFundService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.core.base.context.AdminUserContext;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.vo.AdminUserVO;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/project/fund")
public class ProjectFundController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ProjectFundController.class);

    @Autowired
    private IProjectFundService projectFundService;

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param projectFund
     * @return
     */
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, ProjectFund projectFund) {
        logger.info(projectFund.toString());
        PageQuery<ProjectFund> pageList = projectFundService.pageQuery(new PageQuery<>(pageNumber, pageSize, projectFund));
        pageList.getList().forEach(item -> {
            //翻译创建人
            item.setCreateName(TranslateUtil.getUserName(item.getCreateBy()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 分页查询(流程查询)
     * @param pageNumber
     * @param pageSize
     * @param projectFund
     * @return
     */
    @GetMapping(value = "/listCheck")
    public RtnInfo<?> listCheck(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, ProjectFund projectFund) {
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        List<Long> roleIds = Arrays.stream(adminUserVO.getRoleid().split(",")).mapToLong(Long::valueOf).boxed().collect(Collectors.toList());
        projectFund.setRoleIds(roleIds);
        PageQuery<ProjectFund> pageList = projectFundService.pageQuery(new PageQuery<>(pageNumber, pageSize, projectFund));
        pageList.getList().forEach(item -> {
            //翻译创建人
            item.setCreateName(TranslateUtil.getUserName(item.getCreateBy()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public RtnInfo<?> queryById(@RequestParam Long id) {
        ProjectFund projectFund = projectFundService.selectProjectFundById(id);
        if (null != projectFund) {
            return RtnInfo.success(projectFund);
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 新增
     * @param projectFund
     * @return
     */
    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> add(@RequestBody ProjectFund projectFund) {
        int count = projectFundService.saveProjectFund(projectFund);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 编辑
     * @param projectFund
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody ProjectFund projectFund) {
        int count = projectFundService.updateProjectFund(projectFund);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 重新提交
     * @param projectFund
     * @return
     */
    @PutMapping(value = "/resubmit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> resubmit(@RequestBody ProjectFund projectFund) {
        int count = projectFundService.resubmitProjectFund(projectFund);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        ProjectFund projectFund = new ProjectFund();
        projectFund.setId(id);
        projectFund.setIsDelete(IsDeleteEnum.DELETE.getCode());
        int count = projectFundService.updateById(projectFund);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }
}